package com.anche.demo.aspect;

/**
 * @ClassName: DomainCheckAspect
 * @Description: TODO
 * @date: 2023/1/28 14:35
 * @Auther: hey
 */

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Enumeration;

/**
 * @ClassName: DomainCheckAspect
 * @Description: 校验是否传入域名
 * @date: 2023/1/28 14:24
 * @Auther: hey
 */
@Slf4j
@Aspect
@Component
public class DomainCheckAspect {


    @Pointcut("@annotation(com.anche.demo.aspect.DomainCheck)")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        //获取目标方法
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        DomainCheck domainCheck = method.getAnnotation(DomainCheck.class);
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        String domain = request.getHeader("domain");
        log.info("domain", domain);
        Enumeration<String> headerNames = request.getHeaderNames();
        if(headerNames.hasMoreElements()){
            String s = headerNames.nextElement();
            if(s.equals("domain")){
                s.replace(s,s+"user/test");
            }
        }
        if (null == domain) {
            throw new Exception("");
        } else {
            return point.proceed();
        }
    }

}